﻿using PloobsEngine.SceneControl;
using PloobsEngine.Physics;
using PloobsEngine;
using PloobsEngine.Cameras;
using PloobsEngine.Input;
using Microsoft.Xna.Framework;
using PloobsEngine.Physics.Bepu;
using PloobsEngine.Modelo;
using PloobsEngine.Material;
using Microsoft.Xna.Framework.Graphics;
using PloobsEngine.Features;
using PloobsEngineDemo3;
using PloobsEngine.Light;
using PloobsEngine.Draw;

namespace Etapa1.Screens
{
    /// <summary>
    /// Drawing Screen
    /// </summary>
    public class DrawingScreen : SceneScreen
    {
        private BepuPhysicWorld physicWorld;
        private BrutalForceWorld mundo;
        private EngineStuff engine;        
        ICamera cam;
        FPSCounter counter;

        public DrawingScreen(EngineStuff es)
        {
            this.engine = es;
            physicWorld = new BepuPhysicWorld();
            mundo = new BrutalForceWorld(physicWorld);
            counter = new FPSCounter(es);
        }
        
        public override void LoadContent(EngineStuff es)
        {            
            InputAdvanced inp = new InputAdvanced(engine);
            engine.AddComponent(inp);

            engine.AddComponent(counter);

            base.LoadContent(es);            

            #region Models

            {
                SimpleModel sm = new SimpleModel("..\\Content\\Model\\cenario");
                sm.LoadModelo();
                IPhysicObject pi = new TriangleMeshObject(Vector3.Zero, sm);
                NormalDeferred shader = new NormalDeferred();
                shader.Initialize();
                IMaterial mat = new Material(shader, MaterialType.DEFERRED);
                IObject obj3 = new SimpleObject(mat, pi, sm);
                mundo.AddObject(obj3);
            }
            

            #endregion            

            cam = new CameraFirstPerson(true);            
            cam.FarPlane = 4000;

            LightThrowBepu lt = new LightThrowBepu(mundo, engine);         

            #region NormalLight
            DirectionalLight ld1 = new DirectionalLight(Vector3.Left, Color.White);
            DirectionalLight ld2 = new DirectionalLight(Vector3.Right, Color.White);
            DirectionalLight ld3 = new DirectionalLight(Vector3.Backward, Color.White);
            DirectionalLight ld4 = new DirectionalLight(Vector3.Forward, Color.White);
            DirectionalLight ld5 = new DirectionalLight(Vector3.Down, Color.White);
            float li = 0.4f;
            ld1.LightIntensity = li;
            ld2.LightIntensity = li;
            ld3.LightIntensity = li;
            ld4.LightIntensity = li;
            ld5.LightIntensity = li;
            mundo.AddLight(ld1);
            mundo.AddLight(ld2);
            mundo.AddLight(ld3);
            mundo.AddLight(ld4);
            mundo.AddLight(ld5);
            #endregion            

            
            ////Criacao de uma Tecnica
            DeferredRenderTechnic dr = new DeferredRenderTechnic(engine);
            
            IRenderTechnic[] rt = new IRenderTechnic[] { dr };
            this.RenderTechnics = rt;
            mundo.AddCamera(cam);
            this.World = mundo;

            ///The functions to Draw the objects (like Drawing.Draw2dTextAt2dLocation ) must be called ALL frames            
            ///IF set to false, we need to call it only once
            Drawing.setClearEveryFrame(true);

        }
        public override void Draw(GameTime gameTime)
        {   
         
            ///These functions can be called from EVERYWHERE, EVERYTIME
            ///The objects created are not affected by the lights

            ///Basic 2D Text
            Drawing.Draw2dTextAt2dLocation("FPS Draw " + counter.DrawFPS, new Vector2(40, 20), Color.White);
            Drawing.Draw2dTextAt2dLocation("FPS Update " + counter.UpdateFPS, new Vector2(40, 40), Color.White);
            ///Fake 3D text
            Drawing.Draw2dTextAt3dLocation("Abobrinha Voadora", new Vector3(350, 100, 50),Color.Blue,mundo.ActiveCamera);

            ///Box WireFrame
            Drawing.Draw3dBoxWireFrame(Color.Red, new Vector3(-150, 50, -150), 10);

            ///Circle and Disks
            Drawing.Draw3dCircle(25, new Vector3(200, 50, 100), Vector3.Up, Color.Yellow, 100);
            Drawing.Draw3dCircleOrDisk(25, new Vector3(200, 60, 100), Vector3.Up, Color.YellowGreen, 100,true);

            ///Other way to do the same
            Drawing.DrawCircleOrDisk(25, Vector3.Up + Vector3.Right, new Vector3(200, 150, 100), Color.Red, 100, true, true);
            Drawing.DrawXZCircleOrDisk(10, new Vector3(-130, 60, 140), Color.White, 100, true);

            ///this is 2D (2D is this context MEANS binded to the xz plane, the rotation is not used)
            Drawing.DrawCircleOrDisk(25, Vector3.Up + Vector3.Right, new Vector3(200, 200, 100), Color.DarkGreen, 100, true, false);            
            
            ///Lines
            Drawing.Draw3dLine(new Vector3(250, 0, 250), new Vector3(250, 1000, 250), Color.Blue);
            Drawing.Draw3dLineAlpha(new Vector3(260, 0, 260), new Vector3(250, 1000, 250), Color.DarkBlue, 0.5f);                        

            base.Draw(gameTime);
        }

    }
}

